package com.yiqi.liebang.feature.order.view;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.util.DateTool;
import com.yiqi.liebang.common.widget.RatingBar;
import com.yiqi.liebang.entity.bo.ExpertTopicBo;
import com.yiqi.liebang.entity.bo.TopicOrderInfoBo;
import com.yiqi.liebang.entity.enums.TopicExpertOfflineStauts;
import com.yiqi.liebang.entity.enums.TopicExpertOnlineStauts;
import com.yiqi.liebang.entity.enums.TopicStudentOfflineStauts;
import com.yiqi.liebang.entity.enums.TopicStudentOnlineStauts;
import com.yiqi.liebang.feature.home.view.ClassifyActivity;
import com.yiqi.liebang.feature.mine.view.UserInfoActivity;
import com.yiqi.liebang.feature.order.PhoneDialog;
import com.yiqi.liebang.feature.order.contract.TopicInfoContract;
import com.yiqi.liebang.feature.order.dependencies.topicinfo.DaggerTopicInfoComponent;
import com.yiqi.liebang.feature.order.dependencies.topicinfo.TopicInfoPresenterModule;
import com.yiqi.liebang.feature.order.view.adapter.StepAdapter;
import com.yiqi.liebang.framework.DataServer;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.RongIM;

import static com.yiqi.liebang.feature.order.view.adapter.QuestionAdapter.getDatePoor;

public class OrderInfoActivity extends BaseActivity implements TopicInfoContract.View, Toolbar.OnMenuItemClickListener {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.rv_step)
    RecyclerView mRvStep;
    @BindView(R.id.tv_topic_info_stauts)
    TextView mTvTopicInfoStauts;
    @BindView(R.id.tv_topic_info_date)
    TextView mTvTopicInfoDate;
    @BindView(R.id.tv_topic_info_type)
    TextView mTvTopicInfoType;
    @BindView(R.id.tv_topic_info_title)
    TextView mTvTopicInfoTitle;
    @BindView(R.id.tv_topic_info_help_num)
    TextView mTvTopicInfoHelpNum;
    @BindView(R.id.tv_topic_info_times)
    TextView mTvTopicInfoTimes;
    @BindView(R.id.type_topic_info_star)
    RatingBar mTypeTopicInfoStar;
    @BindView(R.id.tv_topic_info_prize)
    TextView mTvTopicInfoPrize;
    @BindView(R.id.tv_topic_info_original_prize)
    TextView mTvTopicInfoOriginalPrize;
    @BindView(R.id.tv_topic_info_help_question)
    TextView mTvTopicInfoHelpQuestion;
    @BindView(R.id.tv_topic_info_help_introduce)
    TextView mTvTopicInfoHelpIntroduce;
    @BindView(R.id.tv_topic_info_order_no)
    TextView mTvTopicInfoOrderNo;
    @BindView(R.id.tv_topic_info_order_date)
    TextView mTvTopicInfoOrderDate;
    @BindView(R.id.btn_topic_ok)
    TextView mBtnTopicOk;
    StepAdapter mStepAdapter;
    @BindView(R.id.btn_topic_message)
    TextView mBtnTopicMessage;
    @BindView(R.id.view_topic_expert_info)
    LinearLayout mViewTopicExpertInfo;
    @BindView(R.id.btn_topic_order_time)
    TextView mBtnTopicOrderTime;
    @BindView(R.id.view_topic_order_time)
    RelativeLayout mViewTopicOrderTime;
    @BindView(R.id.iv_topic_head)
    ImageView mIvTopicHead;
    @BindView(R.id.tv_user_topic_name)
    TextView mTvUserTopicName;
    @BindView(R.id.iv_user_certification)
    ImageView mIvUserCertification;
    @BindView(R.id.tv_user_topic_position)
    TextView mTvUserTopicPosition;
    @BindView(R.id.iv_user_position)
    ImageView mIvUserPosition;
    @BindView(R.id.tv_question_help_num)
    TextView mTvQuestionHelpNum;
    @BindView(R.id.star)
    RatingBar mStar;
    @BindView(R.id.view_bottom)
    LinearLayout mViewBottom;
    @BindView(R.id.view_position)
    LinearLayout mViewPosition;
    @BindView(R.id.tv_topic_meet_start_time)
    TextView mTvTopicMeetStartTime;
    @BindView(R.id.tv_topic_meet_end_time)
    TextView mTvTopicMeetEndTime;
    @BindView(R.id.tv_topic_meet_start_position)
    TextView mTvTopicMeetStartPosition;
    @BindView(R.id.tv_topic_meet_start_address)
    TextView mTvTopicMeetStartAddress;
    @BindView(R.id.btn_topic_meeting_update)
    TextView mBtnTopicMeetingUpdate;
    @BindView(R.id.view_topic_meeting_update)
    RelativeLayout mViewTopicMeetingUpdate;
    @BindView(R.id.cbx_order_choice)
    ImageView mCbxOrderChoice;
    @BindView(R.id.cbx_order_unchoice)
    ImageView mCbxOrderUnchoice;
    @BindView(R.id.btn_order_unchoice)
    LinearLayout mBtnOrderUnchoice;
    @BindView(R.id.rb_queestion_comment_star)
    RatingBar mRbQueestionCommentStar;
    @BindView(R.id.tv_show_star)
    TextView mTvShowStar;
    @BindView(R.id.btn_confrim_comment)
    TextView mBtnConfrimComment;
    @BindView(R.id.view_to_comment)
    LinearLayout mViewToComment;
    @BindView(R.id.tv_show_comment_user)
    TextView mTvShowCommentUser;
    @BindView(R.id.tv_show_comment_user_content)
    TextView mTvShowCommentUserContent;
    @BindView(R.id.edt_topic_comment_content)
    EditText mEdtTopicCommentContent;
    @BindView(R.id.view_comment_content)
    LinearLayout mViewCommentContent;
    @BindView(R.id.view_bottom_all)
    LinearLayout mViewBottomAll;
    @BindView(R.id.btn_topic_call)
    ImageView mBtnTopicCall;
    @BindView(R.id.btn_order_choice)
    LinearLayout mBtnOrderChoice;
    @BindView(R.id.tv_show_question_title)
    TextView mTvShowQuestionTitle;
    @BindView(R.id.tv_show_intro_title)
    TextView mTvShowIntroTitle;


    private String mTopicOrderId;

    private boolean mIsExpert;
    TopicOrderInfoBo mData;
    private float mScore;
    ExpertTopicBo mExpertTopicBo = null;
    @Inject
    TopicInfoContract.Presenter mPresenter;
    RxPermissions rxPermissions; // where this is an Activity or Fragment instance

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_order_info;
    }

    @Override
    protected void initInjector() {
        DaggerTopicInfoComponent.builder().topicInfoPresenterModule(new TopicInfoPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        rxPermissions = new RxPermissions(this);
        mTopicOrderId = this.getIntent().getStringExtra("id");
        mIsExpert = this.getIntent().getBooleanExtra("is_expert", false);
        initToolBar(mToolbar, "订单详情", true, true);
        mToolbar.setOnMenuItemClickListener(this);
    }

    @Override
    protected void initData() {
        initStepView();
        mPresenter.getOrderQuestionInfo(mTopicOrderId, mIsExpert ? 4 : 1);
    }

    @Override
    protected void onStart() {
        super.onStart();


    }

    private void initStepView() {

        mRvStep.setLayoutManager(new GridLayoutManager(this, 4));
        mStepAdapter = new StepAdapter();
        mStepAdapter.bindToRecyclerView(mRvStep);

    }


    @Override
    public void showOrderTopicInfoSuccess(TopicOrderInfoBo infofBo) {
        if (infofBo != null) {
            mData = infofBo;
            initOrderData(infofBo);
        }
    }

    @Override
    public void showRefusedQuestion(String s) {
        showMsg("已忽略！");
        finish();
    }

    @Override
    public void showExpertConfrim(String s) {
        T.showShort("确认成功！");
        finish();
    }

    @Override
    public void showStudentConfrim(String s) {
        T.showShort("确认成功！");
        finish();
    }


    @Override
    public void showCancleSuccess(String s) {
        showMsg("取消成功！");
        finish();
    }

    @Override
    public void showCommentSuccess(String s) {
        showMsg("点评成功！");
        showToast();
        mViewCommentContent.setVisibility(View.GONE);
        mStepAdapter.setNewData(DataServer.getOrderStautYYPJ());
        mViewTopicExpertInfo.setVisibility(View.VISIBLE);
        mViewBottom.setVisibility(View.GONE);
        mViewBottomAll.setVisibility(View.GONE);
    }

    @Override
    public void showOrderTopicInfoError(String error) {
        showMsg(error);
    }

    @Override
    public void showStudentComplete() {
        showMsg("学员确认成功！");
        mTvTopicInfoStauts.setText("服务已完成");
        mStepAdapter.setNewData(DataServer.getOrderStautYYWC());
        mViewCommentContent.setVisibility(View.VISIBLE);
        mTvShowCommentUser.setText("给" + mData.getTopicFormMap().getTopicUserAccount().getUserName() + "点评");
        mTvShowCommentUserContent.setText(Html.fromHtml("综合评价<font color=\"#00ADEF\">" + mData.getTopicFormMap().getTopicUserAccount().getUserName() + "</font>（建议结合具体事例点评）"));
        mViewBottom.setVisibility(View.GONE);
        mViewToComment.setVisibility(View.VISIBLE);
        mRbQueestionCommentStar.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float ratingCount) {
                mScore = ratingCount;
                if (ratingCount > 0) {
                    mTvShowStar.setVisibility(View.VISIBLE);
                    mBtnConfrimComment.setVisibility(View.VISIBLE);
                } else {
                    mTvShowStar.setVisibility(View.GONE);
                    mBtnConfrimComment.setVisibility(View.GONE);
                }
                if (ratingCount == 1) {
                    mTvShowStar.setText("非常不满意");
                } else if (ratingCount == 2) {
                    mTvShowStar.setText("不满意");
                } else if (ratingCount == 3) {
                    mTvShowStar.setText("一般般");
                } else if (ratingCount == 4) {
                    mTvShowStar.setText("较满意");
                } else if (ratingCount == 5) {
                    mTvShowStar.setText("非常满意");
                }
            }

        });
    }

    @Override
    public void showExpertComplete() {
        showMsg(mIsExpert ? "行家确认成功！" : "学员确认成功！");
        finish();
    }

    @Override
    public void showRemindExpertsSuccess() {
        showMsg(mIsExpert ? "提醒学员确认成功！" : "提醒行家确认成功！");
        finish();
    }

    private void initOrderData(TopicOrderInfoBo info) {

        if (mIsExpert) {
            mTvShowQuestionTitle.setText("他请教的问题");
            mTvShowIntroTitle.setText("他的个人简介");
        } else {
            mTvShowQuestionTitle.setText("我请教的问题");
            mTvShowIntroTitle.setText("我的个人简介");
        }
        mTvTopicInfoStauts.setText(mIsExpert ? info.getTopicFormMap().getServiceType() == 0 ? TopicExpertOfflineStauts.getName(info.getOrderStates())
                : TopicExpertOnlineStauts.getName(info.getOrderStates()) : info.getTopicFormMap().getServiceType() == 0 ?
                TopicStudentOfflineStauts.getName(info.getOrderStates()) : TopicStudentOnlineStauts.getName(info.getOrderStates()));
        mTvTopicInfoTitle.setText(info.getTopicFormMap().getTopicName());
        mTvTopicInfoType.setText(info.getTopicFormMap().getServiceType() == 0 ? "线下约见" : "全国通话");
        mTvTopicInfoHelpNum.setText("帮助过" + info.getTopicFormMap().getTopicUserAccount().getHelpNum() + "人");
        mTvTopicInfoTimes.setText(info.getTopicFormMap().getServiceTime() + "小时/次");
        mTypeTopicInfoStar.setStar(info.getTopicFormMap().getStartLevel());
        mTvTopicInfoPrize.setText(info.getTopicFormMap().getTopicPrice() + "元/次");
        mTvTopicInfoOriginalPrize.setText(info.getTopicFormMap().getOriginalPrice() + "元/次");
        mTvTopicInfoHelpQuestion.setText(info.getTopicAskQuestion());
        mTvTopicInfoHelpIntroduce.setText(info.getAskIntroduction());
        mTvTopicInfoOrderNo.setText("订单编号：" + info.getOrderUid());
        mTvTopicInfoOrderDate.setText("发起时间：" + DateTool.TimeStamp2Date(info.getCreateTime()));
        mTvTopicInfoType.setBackgroundResource(info.getTopicFormMap().getServiceType() == 0 ? R.drawable.list_button_xianxia : R.drawable.list_button_quanguo);
        //对方用户信息
        AM.image().bindToCircleObject(mIsExpert ? mData.getStudentUser().getUserHead() : mData.getTopicFormMap().getTopicUserAccount().getUserHead(), mIvTopicHead);
        mTvUserTopicName.setText(mIsExpert ? mData.getStudentUser().getUserName() : mData.getTopicFormMap().getTopicUserAccount().getUserName());
        mTvUserTopicPosition.setText(mIsExpert ? mData.getStudentUser().getPosition() : mData.getTopicFormMap().getTopicUserAccount().getPosition());
        mTvQuestionHelpNum.setText(mIsExpert ? "帮助过" + mData.getStudentUser().getHelpNum() + "人" : "帮助过" + mData.getTopicFormMap().getTopicUserAccount().getHelpNum() + "人");
        mBtnTopicMeetingUpdate.setText(info.getTopicFormMap().getServiceType() == 0 ? "修改预约时间地点" : "修改预约时间");
        mStar.setStar(mData.getTopicFormMap().getStartLevel());
        mStar.setVisibility(mIsExpert ? View.GONE : View.VISIBLE);

        if (mIsExpert) {
            if (mData.getStudentUser().getIsBasic() == 1) {
                mIvUserCertification.setVisibility(View.VISIBLE);
            } else {
                mIvUserCertification.setVisibility(View.GONE);
            }
            mIvUserPosition.setVisibility(mData.getStudentUser().getIsOccupation() == 1 ? View.VISIBLE : View.GONE);
        } else {
            if (mData.getTopicFormMap().getTopicUserAccount().getIsBasic() == 1) {
                mIvUserCertification.setVisibility(View.VISIBLE);
            } else {
                mIvUserCertification.setVisibility(View.GONE);
            }
            mIvUserPosition.setVisibility(mData.getTopicFormMap().getTopicUserAccount().getIsOccupation() == 1 ? View.VISIBLE : View.GONE);
        }


        switch (info.getOrderStates()) {
            case 1:
                mTvTopicInfoDate.setText(getDatePoor(info.getEndTime(), System.currentTimeMillis()));
                if (mIsExpert) {
                    mViewTopicOrderTime.setVisibility(View.VISIBLE);
                    mBtnTopicOrderTime.setText(info.getTopicFormMap().getServiceType() == 0 ? "预约时间地点" : "预约时间");
                    mCbxOrderChoice.setVisibility(View.INVISIBLE);
                    mBtnOrderUnchoice.setVisibility(View.GONE);
                    mBtnTopicMessage.setVisibility(View.VISIBLE);
                    mToolbar.inflateMenu(R.menu.menu_askinfo_hulue);
                    mStepAdapter.setNewData(DataServer.getOrderStautQR());
                    mViewTopicExpertInfo.setVisibility(View.VISIBLE);

                } else {
                    mBtnTopicMessage.setVisibility(View.GONE);
                    mToolbar.inflateMenu(R.menu.menu_cancal);
                    mBtnTopicOk.setText("提醒行家确认");
                    mStepAdapter.setNewData(DataServer.getOrderStautYYFK());
                }
                break;
            case 8:
                mViewBottom.setVisibility(View.GONE);
                mStepAdapter.setNewData(DataServer.getOrderStautYYFK());
//                mTvTopicInfoDate.setVisibility(View.VISIBLE);
                if (mIsExpert) {
                    mTvTopicInfoDate.setText("学员因个人原因取消了预约，预约已无效");

                } else {
                    mTvTopicInfoDate.setText("取消原因：个人原因，预约已无效");
                }
                break;
            case 11:
                mTvTopicInfoDate.setText(getDatePoor(info.getEndTime(), System.currentTimeMillis()) + "服务完成");
                mStepAdapter.setNewData(DataServer.getOrderStautYYWC());
                mViewTopicExpertInfo.setVisibility(View.VISIBLE);
                mViewPosition.setVisibility(View.VISIBLE);
                mTvTopicMeetStartTime.setText(Html.fromHtml("开始时间：<font color=\"#111111\">" + DateTool.TimeStamp2Date(info.getMettingBeginTime()) + "</font>"));
                mTvTopicMeetEndTime.setText(Html.fromHtml("结束时间：<font color=\"#111111\">" + DateTool.TimeStamp2Date(info.getMettingEdnTime()) + "</font>"));
                mTvTopicMeetStartPosition.setText(Html.fromHtml("地\t\t\t\t\t点：<font color=\"#111111\">" + info.getMettingAddress() + "</font>"));
                mTvTopicMeetStartAddress.setText(Html.fromHtml("详细地址：<font color=\"#111111\">" + info.getDetailedAddress() + "</font>"));
                if (info.getTopicFormMap().getServiceType() == 1) {
                    mTvTopicMeetStartPosition.setVisibility(View.GONE);
                    mTvTopicMeetStartAddress.setVisibility(View.GONE);
                }

                mCbxOrderChoice.setVisibility(View.INVISIBLE);
                mBtnOrderUnchoice.setVisibility(View.GONE);
                mBtnTopicMeetingUpdate.setVisibility(View.GONE);
                mBtnTopicMessage.setVisibility(View.VISIBLE);
                if (mIsExpert) {
                    mBtnTopicOk.setText("去TA的主页");
                } else {
                    mBtnTopicMessage.setVisibility(View.GONE);
                    mBtnTopicOk.setText("确认完成");
                }
                break;
            case 4:
                mTvTopicInfoDate.setText(getDatePoor(info.getEndTime(), System.currentTimeMillis()) + "服务完成");
                mStepAdapter.setNewData(DataServer.getOrderStautQR());
                mViewTopicExpertInfo.setVisibility(View.VISIBLE);
                mViewPosition.setVisibility(View.VISIBLE);
                mTvTopicMeetStartTime.setText(Html.fromHtml("开始时间：<font color=\"#111111\">" + DateTool.TimeStamp2Date(info.getMettingBeginTime()) + "</font>"));
                mTvTopicMeetEndTime.setText(Html.fromHtml("结束时间：<font color=\"#111111\">" + DateTool.TimeStamp2Date(info.getMettingEdnTime()) + "</font>"));
                mTvTopicMeetStartPosition.setText(Html.fromHtml("地\t\t\t\t\t点：<font color=\"#111111\">" + info.getMettingAddress() + "</font>"));
                mTvTopicMeetStartAddress.setText(Html.fromHtml("详细地址：<font color=\"#111111\">" + info.getDetailedAddress() + "</font>"));
                if (info.getTopicFormMap().getServiceType() == 1) {
                    mTvTopicMeetStartPosition.setVisibility(View.GONE);
                    mTvTopicMeetStartAddress.setVisibility(View.GONE);
                }

                mCbxOrderChoice.setVisibility(View.INVISIBLE);
                mBtnOrderUnchoice.setVisibility(View.GONE);
                mBtnTopicMeetingUpdate.setVisibility(View.GONE);
                mBtnTopicMessage.setVisibility(View.VISIBLE);
                if (mIsExpert) {
                    mBtnTopicOk.setText("去TA的主页");
                } else {
                    mViewCommentContent.setVisibility(View.VISIBLE);
                    mTvShowCommentUser.setText("给" + mData.getTopicFormMap().getTopicUserAccount().getUserName() + "点评");
                    mTvShowCommentUserContent.setText(Html.fromHtml("综合评价<font color=\"#00ADEF\">" + info.getTopicFormMap().getTopicUserAccount().getUserName() + "</font>（建议结合具体事例点评）"));
                    mViewBottom.setVisibility(View.GONE);
                    mViewToComment.setVisibility(View.VISIBLE);
                    mRbQueestionCommentStar.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
                        @Override
                        public void onRatingChange(float ratingCount) {
                            mScore = ratingCount;
                            if (ratingCount > 0) {
                                mTvShowStar.setVisibility(View.VISIBLE);
                                mBtnConfrimComment.setVisibility(View.VISIBLE);
                            } else {
                                mTvShowStar.setVisibility(View.GONE);
                                mBtnConfrimComment.setVisibility(View.GONE);
                            }
                            if (ratingCount == 1) {
                                mTvShowStar.setText("非常不满意");
                            } else if (ratingCount == 2) {
                                mTvShowStar.setText("不满意");
                            } else if (ratingCount == 3) {
                                mTvShowStar.setText("一般般");
                            } else if (ratingCount == 4) {
                                mTvShowStar.setText("较满意");
                            } else if (ratingCount == 5) {
                                mTvShowStar.setText("非常满意");
                            }
                        }
                    });
                }
                break;
            case 3:
                mStepAdapter.setNewData(DataServer.getOrderStautYYPJ());
                mViewTopicExpertInfo.setVisibility(View.VISIBLE);
                mViewBottom.setVisibility(View.GONE);
                mViewBottomAll.setVisibility(View.GONE);
//                showToast();
                break;
            case 7:

                break;
            case 6:
                if (mIsExpert) {
                    mTvTopicInfoDate.setText("预约失效");
                    mViewBottom.setVisibility(View.GONE);
                } else {

                    mBtnTopicOk.setText("预约其他行家");
                    mTvTopicInfoDate.setText("行家因个人原因无法确认约见，您的退款费用将返还至原账户");
                }

                mStepAdapter.setNewData(DataServer.getOrderStautYYFK());
                break;
            case 9:
                mExpertTopicBo = new ExpertTopicBo(mTopicOrderId,
                        DateTool.TimeStamp2Date(info.getMettingBeginTime()),
                        DateTool.TimeStamp2Date(info.getMettingEdnTime()),
                        info.getMettingAddress(),
                        info.getDetailedAddress());
                mBtnTopicOk.setText("确认预约");
                mBtnTopicMessage.setVisibility(View.VISIBLE);
                mStepAdapter.setNewData(DataServer.getOrderStautQR());
                mViewTopicExpertInfo.setVisibility(View.VISIBLE);
                mViewPosition.setVisibility(View.VISIBLE);
                mTvTopicInfoDate.setText(getDatePoor(info.getEndTime(), System.currentTimeMillis()));
                mTvTopicMeetStartTime.setText(Html.fromHtml("开始时间：<font color=\"#111111\">" + DateTool.TimeStamp2Date(info.getMettingBeginTime()) + "</font>"));
                mTvTopicMeetEndTime.setText(Html.fromHtml("结束时间：<font color=\"#111111\">" + DateTool.TimeStamp2Date(info.getMettingEdnTime()) + "</font>"));
                mTvTopicMeetStartPosition.setText(Html.fromHtml("地\t\t\t\t\t点：<font color=\"#111111\">" + info.getMettingAddress() + "</font>"));
                mTvTopicMeetStartAddress.setText(Html.fromHtml("详细地址：<font color=\"#111111\">" + info.getDetailedAddress() + "</font>"));

                if (info.getTopicFormMap().getServiceType() == 1) {
                    mTvTopicMeetStartPosition.setVisibility(View.GONE);
                    mTvTopicMeetStartAddress.setVisibility(View.GONE);
                }

                if (mIsExpert) {
                    mCbxOrderChoice.setVisibility(View.INVISIBLE);
                    mBtnOrderUnchoice.setVisibility(View.GONE);
                } else {
                    mBtnTopicMeetingUpdate.setVisibility(View.GONE);
                }
                break;
            case 10:
                mStepAdapter.setNewData(DataServer.getOrderStautQR());
                mViewTopicExpertInfo.setVisibility(View.VISIBLE);
                mViewPosition.setVisibility(View.VISIBLE);
                mTvTopicMeetStartTime.setText(Html.fromHtml("开始时间：<font color=\"#111111\">" + DateTool.TimeStamp2Date(info.getMettingBeginTime()) + "</font>"));
                mTvTopicMeetEndTime.setText(Html.fromHtml("结束时间：<font color=\"#111111\">" + DateTool.TimeStamp2Date(info.getMettingEdnTime()) + "</font>"));
                mTvTopicMeetStartPosition.setText(Html.fromHtml("地\t\t\t\t\t点：<font color=\"#111111\">" + info.getMettingAddress() + "</font>"));
                mTvTopicMeetStartAddress.setText(Html.fromHtml("详细地址：<font color=\"#111111\">" + info.getDetailedAddress() + "</font>"));
                if (info.getTopicFormMap().getServiceType() == 1) {
                    mTvTopicMeetStartPosition.setVisibility(View.GONE);
                    mTvTopicMeetStartAddress.setVisibility(View.GONE);
                }

                mCbxOrderChoice.setVisibility(View.INVISIBLE);
                mBtnOrderUnchoice.setVisibility(View.GONE);
                mBtnTopicMeetingUpdate.setVisibility(View.GONE);
                mBtnTopicMessage.setVisibility(View.VISIBLE);
                mBtnTopicOk.setText("服务完成");
                break;

        }

    }


    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if (item.getItemId() == R.id.action_cancal) {
            mPresenter.cancleOrder(mTopicOrderId);
        } else if (item.getItemId() == R.id.action_hulue) {
            mPresenter.refusedQuestion(mData.getId());
        }
        return false;
    }


    @OnClick({R.id.btn_topic_order_time, R.id.btn_topic_message, R.id.btn_topic_ok,
            R.id.btn_topic_call, R.id.btn_topic_meeting_update, R.id.btn_order_choice,
            R.id.btn_order_unchoice, R.id.btn_confrim_comment})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_topic_order_time:
                try {
                    Intent intent = new Intent(this, TakeTimeActivity.class);
                    intent.putExtra("id", mData);
                    intent.putExtra("is_online", mData.getTopicFormMap().getServiceType() == 0 ? false : true);
                    startActivityForResult(intent, 2);
                } catch (Exception e) {
                    T.showShort(e.getMessage());
                }
                break;
            case R.id.btn_topic_message:
                if (mData != null)
                    try {
                        RongIM.getInstance().startPrivateChat(this,
                                mData.getTopicFormMap().getTopicUserAccount().getUserUid(),
                                mData.getTopicFormMap().getTopicUserAccount().getUserName());
                    } catch (Exception e) {
                        T.showShort(e.getMessage());
                    }

                break;
            case R.id.btn_topic_ok:
                switch (mData.getOrderStates()) {
                    case 1:
                        if (mIsExpert) {
                            if (mExpertTopicBo != null) {
                                mPresenter.toExpertConfrim(mExpertTopicBo);
                            } else {
                                T.showShort("请选择预约时间地点！");
                            }
                        } else {
                            mPresenter.remindExpertsConfirm(mTopicOrderId);
                        }

                        break;
                    case 4:
                        if (mIsExpert) {
                            Intent intent = new Intent(this, UserInfoActivity.class);
                            intent.putExtra("is_view_my", false);
                            intent.putExtra("visitorUid", mData.getTopicFormMap().getTopicUserAccount().getUserUid());
                            startActivity(intent);
                        } else {

                        }
                        break;
                    case 9:
                        if (mIsExpert) {
                            if (mExpertTopicBo != null) {
                                mPresenter.toExpertConfrim(mExpertTopicBo);
                            } else {
                                T.showShort("请选择预约时间地点！");
                            }
                        } else {
                            mPresenter.toStudentConfrim(mTopicOrderId);
                        }
                        break;
                    case 10:
                        if (mIsExpert) {
                            mPresenter.toExpertComplete(mTopicOrderId);
                        } else {
                            mPresenter.toStudentComplete(mTopicOrderId);
                        }
                        break;
                    case 11:
                        if (mIsExpert) {
                            Intent intent = new Intent(this, UserInfoActivity.class);
                            intent.putExtra("is_view_my", false);
                            intent.putExtra("visitorUid", mData.getTopicFormMap().getTopicUserAccount().getUserUid());
                            startActivity(intent);

                        } else {
                            mPresenter.toStudentComplete(mTopicOrderId);
                        }
                        break;
                    case 6:
                        startActivity(new Intent(this, ClassifyActivity.class));
                        break;
                }

                break;
            case R.id.btn_topic_call:
                if (!TextUtils.isEmpty(mData.getTopicFormMap().getTopicUserAccount().getUserPhone())) {
                    PhoneDialog topicListDialog = new PhoneDialog(this, mIsExpert ? mData.getStudentUser().getUserPhone() : mData.getTopicFormMap().getTopicUserAccount().getUserPhone());
                    topicListDialog.show();
                    topicListDialog.setDialClick(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            rxPermissions
                                    .request(Manifest.permission.CALL_PHONE)
                                    .subscribe(granted -> {
                                        if (granted) { // Always true pre-M
                                            diallPhone(mData.getTopicFormMap().getTopicUserAccount().getUserPhone());
                                        } else {
                                            showMsg("请先开启拨打电话权限!");
                                        }
                                    });

                        }
                    });
                }
                break;

            case R.id.btn_topic_meeting_update:
                Intent intent = new Intent(this, TakeTimeActivity.class);
                intent.putExtra("id", mData);
                intent.putExtra("meet", mExpertTopicBo);
                intent.putExtra("is_online", mData.getTopicFormMap().getServiceType() == 0 ? false : true);
                startActivityForResult(intent, 2);
                break;
            case R.id.btn_order_choice:
                mBtnTopicOk.setClickable(true);
                mBtnTopicOk.setEnabled(true);
                mBtnTopicOk.setBackgroundColor(getResources().getColor(R.color.primary_highlight));
                mCbxOrderChoice.setImageResource(R.drawable.list_button_checked);
                mCbxOrderUnchoice.setImageResource(R.drawable.list_button_unchecked);
                break;
            case R.id.btn_order_unchoice:
                mBtnTopicOk.setClickable(false);
                mBtnTopicOk.setEnabled(false);
                mBtnTopicOk.setBackgroundColor(getResources().getColor(R.color.div_primary));
                mCbxOrderChoice.setImageResource(R.drawable.list_button_unchecked);
                mCbxOrderUnchoice.setImageResource(R.drawable.list_button_checked);
                break;

            case R.id.btn_confrim_comment:
                mPresenter.toCommentTopicOrder(mData.getTopicFormMap().getTopicUserAccount().getUserUid()
                        , mEdtTopicCommentContent.getText().toString(),
                        mScore, mTopicOrderId
                );
                break;
        }
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public void diallPhone(String phoneNum) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        //url:统一资源定位符
        //uri:统一资源标示符（更广）
        intent.setData(Uri.parse("tel:" + phoneNum));
        //开启系统拨号器
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 2) {
            mExpertTopicBo = (ExpertTopicBo) data.getSerializableExtra("meet");
            if (mExpertTopicBo != null) {
                mViewPosition.setVisibility(View.VISIBLE);
                mBtnTopicOrderTime.setVisibility(View.GONE);
                mTvTopicMeetStartTime.setText(Html.fromHtml("开始时间：<font color=\"#111111\">" + mExpertTopicBo.getMettingBeginTime() + "</font>"));
                mTvTopicMeetEndTime.setText(Html.fromHtml("结束时间：<font color=\"#111111\">" + mExpertTopicBo.getMettingEdnTime() + "</font>"));
                mTvTopicMeetStartPosition.setText(Html.fromHtml("地\t\t\t\t\t点：<font color=\"#111111\">" + mExpertTopicBo.getMettingAddress() + "</font>"));
                mTvTopicMeetStartAddress.setText(Html.fromHtml("详细地址：<font color=\"#111111\">" + mExpertTopicBo.getDetailedAddress() + "</font>"));
                if (mData.getTopicFormMap().getServiceType() == 1) {
                    mTvTopicMeetStartPosition.setVisibility(View.GONE);
                    mTvTopicMeetStartAddress.setVisibility(View.GONE);
                }
            }
        }
    }

    private void showToast() {
        View toastView = LayoutInflater.from(this).inflate(R.layout.toast_view, null);
        LinearLayout relativeLayout = (LinearLayout) toastView.findViewById(R.id.toast_linear);
//动态设置toast控件的宽高度，宽高分别是130dp
//这里用了一个将dp转换为px的工具类PxUtil
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        relativeLayout.setLayoutParams(layoutParams);
        TextView textView = (TextView) toastView.findViewById(R.id.tv);
//       textView.setText("已清空小纸条记录");
        Toast toast = new Toast(this);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setView(toastView);
        toast.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
